from bs4 import BeautifulSoup
import requests
import csv

# 发送请求获取网页内容
url = "https://top.baidu.com/board?tab=realtime"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"}
response = requests.get(url, headers=headers)
html = response.text

# 使用BeautifulSoup解析网页
soup = BeautifulSoup(html, 'lxml')

# 查找热搜词和热度指数的元素
hot_word_elements = soup.find_all('div', class_='c-single-text-ellipsis')
hot_index_elements = soup.find_all('div', class_='hot-index_1Bl1a')

# 提取热搜词和热度指数
hot_words = [element.text.strip() for element in hot_word_elements]
hot_indexes = [element.text.strip() for element in hot_index_elements]

# 将数据保存到CSV文件
with open('baidu_hot_search.csv', 'w', newline='', encoding='utf-8-sig') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['热搜词', '热度指数'])
    for word, index in zip(hot_words, hot_indexes):
        writer.writerow([word, index])